#include "BulletObject.h"

int BulletObject::bullet_name = 0;
bool BulletObject::compiled = false;

BulletObject::BulletObject()
{
}

BulletObject::~BulletObject()
{

}

 
void BulletObject::LoadLists()
{
bullet_name = glGenLists(1);    
glNewList(bullet_name,GL_COMPILE);
   
    glColor4f(1.0f, 1.0f, 1.0f, 1);     
    glBlendFunc(GL_ONE,GL_ONE);       
    glEnable(GL_BLEND);         
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_BULLET]);
    glScalef(1.1,0.3,1.1);
    gluSphere(state->quad_obj,0.5,10,10);
    glDisable(GL_BLEND);  
    glDisable(GL_TEXTURE_2D);
glEndList();
compiled = true;
}
 



void BulletObject::DoIdle()
{
  //  printf("\nbullet %.1f - %.1f ",pos.y,state->world->GetHeight(-pos.x,-pos.z));
    if (-pos.y < state->world->GetHeight(-pos.x,-pos.z))
     {
        state->targ->CheckCollision(BULLET,-pos.x,pos.y,-pos.z);
        pos.x += dx*vel;
        pos.z += dz*vel;
        pos.y -= BULLET_GRAVITY;
        vel = BULLET_VELOCITY;
     }
    else
    {
       visible = false;
       Generate_Dust(pos.x ,pos.y,pos.z);
    }
     
  
}


void BulletObject::Init()
{
    //se il proiettile è sparato
    vel = 1;
    visible = true;    
    if (!compiled) LoadLists();
}


void BulletObject::Draw(){
glPushMatrix();
    glTranslatef(pos.x , pos.y ,pos.z);
    glCallList(bullet_name);
glPopMatrix();
}


void BulletObject::SetPosDirection(float x, float y, float z,float angY )
{
    inizio = true;
  pos.x = x;
  pos.y = y;
  pos.z = z; 
  dx = sin(state->cam_angY * PI_DIV_180);   //direzione pesata
  dz = -cos(state->cam_angY * PI_DIV_180);   //direzione pesata
}


void BulletObject::Generate_Dust(float x,float y,float z)
{
   DustObject * s = new  DustObject(state);
   s->Init();
   s->setPos(x,y,z);

  
   bool flag = false;
    for (int i = 0;(!flag && i <= state->dust_counter);i++)
          if (state->dusts[i] == NULL) state->dusts[i] = s,flag = true;
   
   state->dust_counter++;
   if (state->dust_counter > MAX_BULLET_NUM) state->dust_counter = 0;
}



